<?php
/**
 * Created by PhpStorm.
 * User: songdewei
 * Date: 2017/10/17
 * Time: 上午11:52
 */

namespace Model\Member;


use Data\Company\CompanyModel;
use Data\Company\ProductContentModel;
use Data\Company\ProductImageModel;
use Data\Company\ProductModel;

class ProductController extends BaseController
{
    /**
     * ProductController constructor.
     */
    public function __construct()
    {
        parent::__construct();
        $this->var['_menu'] = 'product';
    }

    /**
     *
     */
    public function index(){
        global $_G,$_lang;

        $productModel = new ProductModel();
        if ($this->checkFormSubmit()) {
            $products = $_GET['products'];
            if ($products) {
                $contentModel = new ProductContentModel();
                $imageModel = new ProductImageModel();
                foreach ($products as $product_id){
                    $condition = array('uid'=>$this->uid, 'product_id'=>$product_id);
                    $productModel->where($condition)->delete();
                    $imageModel->where($condition)->delete();
                    $contentModel->where($condition)->delete();
                }
            }
            $this->showSuccess('delete_succeed');
        }else {
            $pagesize = 20;
            $condition = array('uid'=>$this->uid);

            $totalcount = $productModel->where($condition)->count();
            $pagecount  = $totalcount < $pagesize ? 1 : ceil($totalcount/$pagesize);
            $productList = $productModel->where($condition)->page($_G['page'], $pagesize)->order('product_id', 'DESC')->select();
            $pagination = $this->pagination($_G['page'], $pagecount, $totalcount, null, true);

            $this->var['title'] = '产品管理';
            include template('product');
        }
    }

    /**
     *
     */
    public function publish(){
        global $_G,$_lang;

        $company = (new CompanyModel())->where(array('uid'=>$this->uid))->getOne();
        if (!$company) {
            $this->showError('请先添加企业信息');
        }

        $product_id = intval($_GET['product_id']);
        $productModel = new ProductModel();
        $contentModel = new ProductContentModel();
        if ($this->checkFormSubmit()) {
            $product = $_GET['product'];
            if ($product['title']) {
                $content = $_GET['content'];
                $gallery = $_GET['gallery'];
                if ($gallery[0]['thumb'] && $gallery[0]['image']){
                    $product['thumb'] = $gallery[0]['thumb'];
                    $product['image'] = $gallery[0]['image'];
                }
                if ($product_id) {
                    $product['update_time'] = time();
                    $productModel->where(array('uid'=>$this->uid, 'product_id'=>$product_id))->data($product)->save();
                }else {
                    $product['uid'] = $this->uid;
                    $product['create_time'] = time();
                    $product['company_id'] = $company['company_id'];
                    $product_id = $productModel->data($product)->add();
                }
                $res = $contentModel->where(array('uid'=>$this->uid))
                    ->data(array('content'=>$content,'update_time'=>time()))->save();
                if (!$res) {
                    $contentModel->data(array(
                        'uid'=>$this->uid,
                        'product_id'=>$product_id,
                        'content'=>$content,
                        'update_time'=>time()
                    ))->add();
                }

                if ($gallery) {
                    $imageModel = new ProductImageModel();
                    foreach ($gallery as $ga){
                        $id = intval($ga['id']);
                        if ($ga['thumb'] && $ga['image']){
                            if ($id > 0) {
                                $imageModel->where(array('id'=>$id, 'uid'=>$this->uid))->update(array(
                                    'thumb'=>$ga['thumb'],
                                    'image'=>$ga['image']
                                ));
                            }else {
                                $imageModel->add(array(
                                    'uid'=>$this->uid,
                                    'product_id'=>$product_id,
                                    'thumb'=>$ga['thumb'],
                                    'image'=>$ga['image']
                                ));
                            }
                        }
                    }
                }

                $this->showSuccess('save_succeed');
            }
        }else {

            if ($product_id) {
                $product = $productModel->where(array('uid'=>$this->uid, 'product_id'=>$product_id))->getOne();
                $content = (new ProductContentModel())->where(array('uid'=>$this->uid, 'product_id'=>$product_id))->getOne();
                $gallery = (new ProductImageModel())->where(array('uid'=>$this->uid, 'product_id'=>$product_id))->select();
                $editorcontent = $content['content'];
            }
            $editorname = 'content';
            $this->var['title'] = '添加产品';
            include template('product_form');
        }
    }
}